package com.lwkjgf.workplace.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import com.lwkjgf.workplace.R;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 状态栏工具类
 * 作者： JairusTse
 * 日期： 17/12/19
 */
public class StatusBarUtil {

    /**
     * 设置状态栏为透明
     * @param activity
     */
    @TargetApi(19)
    public static void setTranslucentStatus(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 修改状态栏颜色，支持4.4以上版本
     * @param activity
     * @param colorId
     */
    public static void setStatusBarColor(Activity activity, int colorId) {

        //Android6.0（API 23）以上，系统方法
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.setStatusBarColor(activity.getResources().getColor(colorId));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //使用SystemBarTint库使4.4版本状态栏变色，需要先将状态栏设置为透明
            setTranslucentStatus(activity);
            //设置状态栏颜色
            SystemBarTintManager tintManager = new SystemBarTintManager(activity);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(colorId);
        }
    }

    /**
     * 设置状态栏模式
     * @param activity
     * @param isTextDark 文字、图标是否为黑色 （false为默认的白色）
     * @param colorId 状态栏颜色
     * @return
     */
    public static void setStatusBarMode(Activity activity, boolean isTextDark, int colorId) {

        if(!isTextDark) {
            //文字、图标颜色不变，只修改状态栏颜色
            setStatusBarColor(activity, colorId);
        } else {
            //修改状态栏颜色和文字图标颜色
            setStatusBarColor(activity, colorId);
            //4.4以上才可以改文字图标颜色
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if(OSUtil.isMIUI()) {
                    //小米MIUI系统
                    setMIUIStatusBarTextMode(activity, isTextDark);
                } else if(OSUtil.isFlyme()) {
                    //魅族flyme系统
                    setFlymeStatusBarTextMode(activity, isTextDark);
                } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //6.0以上，调用系统方法
                    Window window = activity.getWindow();
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    //设置字体白色
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

                    window.setStatusBarColor(activity.getResources().getColor(android.R.color.white));

                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                } else {
                    //4.4以上6.0以下的其他系统，暂时没有修改状态栏的文字图标颜色的方法，有可以加上
                    setStatusBarColor(activity, R.color.purple_500);
                }
            }
        }
    }

    /**
     * 设置Flyme系统状态栏的文字图标颜色
     * @param activity
     * @param isDark 状态栏文字及图标是否为深色
     * @return
     */
    public static boolean setFlymeStatusBarTextMode(Activity activity, boolean isDark) {
        Window window = activity.getWindow();
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (isDark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * 设置MIUI系统状态栏的文字图标颜色（MIUIV6以上）
     * @param activity
     * @param isDark 状态栏文字及图标是否为深色
     * @return
     */
    public static boolean setMIUIStatusBarTextMode(Activity activity, boolean isDark) {
        boolean result = false;
        Window window = activity.getWindow();
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if (isDark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result = true;

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //开发版 7.7.13 及以后版本采用了系统API，旧方法无效但不会报错，所以两个方式都要加上
                    if (isDark) {
                        activity.getWindow().getDecorView().setSystemUiVisibility(View
                                .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View
                                .SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    } else {
                        activity.getWindow().getDecorView().setSystemUiVisibility(View
                                .SYSTEM_UI_FLAG_VISIBLE);
                    }
                }
            } catch (Exception e) {

            }
        }
        return result;
    }
    /**
     * 设备系统工具类
     * 作者： JairusTse
     * 日期： 17/12/19
     */
    public static class OSUtil{


            //MIUI标识
            private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
            private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
            private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

            //EMUI标识
            private static final String KEY_EMUI_VERSION_CODE = "ro.build.version.emui";
            private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
            private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";

            //Flyme标识
            private static final String KEY_FLYME_ID_FALG_KEY = "ro.build.display.id";
            private static final String KEY_FLYME_ID_FALG_VALUE_KEYWORD = "Flyme";
            private static final String KEY_FLYME_ICON_FALG = "persist.sys.use.flyme.icon";
            private static final String KEY_FLYME_SETUP_FALG = "ro.meizu.setupwizard.flyme";
            private static final String KEY_FLYME_PUBLISH_FALG = "ro.flyme.published";

            /**
             * 是否是Flyme系统
             * @return
             */
            public static boolean isFlyme() {
                if(isPropertiesExist(KEY_FLYME_ICON_FALG, KEY_FLYME_SETUP_FALG, KEY_FLYME_PUBLISH_FALG)) {
                    return true;
                }
                try {
                    BuildProperties buildProperties = BuildProperties.newInstance();
                    if(buildProperties.containsKey(KEY_FLYME_ID_FALG_KEY)) {
                        String romName = buildProperties.getProperty(KEY_FLYME_ID_FALG_KEY);
                        if (!TextUtils.isEmpty(romName) && romName.contains(KEY_FLYME_ID_FALG_VALUE_KEYWORD)) {
                            return true;
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return false;
            }

            /**
             * 是否是EMUI系统
             * @return
             */
            public static boolean isEMUI() {
                return isPropertiesExist(KEY_EMUI_VERSION_CODE, KEY_EMUI_API_LEVEL,
                        KEY_EMUI_CONFIG_HW_SYS_VERSION);
            }

            /**
             * 是否是MIUI系统
             * @return
             */
            public static boolean isMIUI() {
                return isPropertiesExist(KEY_MIUI_VERSION_CODE, KEY_MIUI_VERSION_NAME,
                        KEY_MIUI_INTERNAL_STORAGE);
            }

            private static boolean isPropertiesExist(String... keys) {
                if (keys == null || keys.length == 0) {
                    return false;
                }
                try {
                    BuildProperties properties = BuildProperties.newInstance();
                    for (String key : keys) {
                        String value = properties.getProperty(key);
                        if (value != null)
                            return true;
                    }
                    return false;
                } catch (IOException e) {
                    return false;
                }
            }

            private static final class BuildProperties {

                private final Properties properties;

                private BuildProperties() throws IOException {
                    properties = new Properties();
                    // 读取系统配置信息build.prop类
                    /*properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build" +
                            ".prop")));*/
                }

                public boolean containsKey(final Object key) {
                    return properties.containsKey(key);
                }

                public boolean containsValue(final Object value) {
                    return properties.containsValue(value);
                }

                public Set<Map.Entry<Object, Object>> entrySet() {
                    return properties.entrySet();
                }

                public String getProperty(final String name) {
                    return properties.getProperty(name);
                }

                public String getProperty(final String name, final String defaultValue) {
                    return properties.getProperty(name, defaultValue);
                }

                public boolean isEmpty() {
                    return properties.isEmpty();
                }

                public Enumeration<Object> keys() {
                    return properties.keys();
                }

                public Set<Object> keySet() {
                    return properties.keySet();
                }

                public int size() {
                    return properties.size();
                }

                public Collection<Object> values() {
                    return properties.values();
                }

                public static BuildProperties newInstance() throws IOException {
                    return new BuildProperties();
                }
            }

    }
}